package com.sls.web.manager.action.recharge;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.recharge.querybean.CardInfoQuery;
import com.sls.web.manager.recharge.service.CardInfoService;

/**
 * 导出卡密数据 <br>
 * 已废弃
 * 
 * @author sxf
 * 
 */
@Deprecated
@Results(value = { @Result(name = "filedownload", type = "stream", params = {
		"contentType", "text/plain", "inputName", "istream",
		"contentDisposition", "attachment;filename=${fileName}" }) })
public class CardExportAction extends SuperAction {
	private static final long serialVersionUID = 1L;

	@Resource(name = "cardInfoService")
	private CardInfoService cardInfoService;

	// 页面使用
	private String isSale;// 是否已出售
	private String startdate;// 销售 开始日期
	private String enddate;// 销售 结束日期
	private CardInfoQuery cardInfoQuery;

	// 下载使用
	private String fileName;// 文件名称
	private InputStream istream; // 输入流

	/**
	 * 导出卡密信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String export() throws Exception {
		if (cardInfoQuery == null) {
			cardInfoQuery = new CardInfoQuery();
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date d = new Date();

		if (StringUtils.isBlank(startdate)) {
			startdate = sdf.format(DateAgeOfMonth(d, -1));
		}
		if (StringUtils.isBlank(enddate)) {
			enddate = sdf.format(d);
		}

		cardInfoQuery.setStartSaleDate(startdate);
		cardInfoQuery.setEndSaleDate(enddate);

		if (StringUtils.isNotBlank(isSale)) {
			try {
				cardInfoQuery.setStatus(Integer.parseInt(isSale));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// String filename =
		// cardInfoService.createCardInfoFileTxt(cardInfoQuery,
		// getCurrentOperator());

		// String saleStatusStr = "";
		// if (cardInfoQuery.getStatus() != null) {
		// int saleStatus = cardInfoQuery.getStatus();
		// if (saleStatus == 0) {
		// saleStatusStr = "待销售_";
		// } else if (saleStatus == 1) {
		// saleStatusStr = "已销售_";
		// }
		// }

		// String filenameWeb = "卡密信息_" + saleStatusStr + filename;
		// String baseDir = null;
		// baseDir = ServletActionContext.getServletContext().getRealPath(
		// "/download");
		// baseDir =
		// "F:/SoftwareProject/Eclipse_SLS/service_rechange/target/classes/download";

		// String filePath = baseDir + "/" + filename;

		// downloadfile(filePath, filenameWeb);

		// FileInputStream fis = new FileInputStream(baseDir + "/" + filename);

		// fileName = new String(filenameWeb.getBytes(), "ISO-8859-1");
		// istream = new FileInputStream(baseDir + "/" + filename);

		// istream = new ByteArrayInputStream(filename.getBytes(), 0,
		// filename.length());

		return null;
	}

	public Date DateAgeOfMonth(Date d, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	}

	/**
	 * @param filePath
	 *            文件路径
	 * @param filenameWeb
	 *            返回页面的文件名
	 * @throws Exception
	 */
	/*
	 * public void downloadfile(String filePath, String filenameWeb) throws
	 * Exception { HttpServletResponse response =
	 * ServletActionContext.getResponse(); response.reset(); try { File file =
	 * new File(filePath); if (!file.exists()) { return; }
	 * response.setContentType("application/octet-stream");
	 * response.setHeader("Content-Disposition", "attachment; filename=" + new
	 * String(filenameWeb.getBytes(), "iso-8859-1")); ServletOutputStream out =
	 * null; out = response.getOutputStream();
	 * 
	 * byte[] tempbytes = new byte[1024]; int byteread = 0; FileInputStream fis
	 * = new FileInputStream(file); while ((byteread = fis.read(tempbytes)) !=
	 * -1) { out.write(tempbytes, 0, byteread); } fis.close(); if
	 * (file.exists()) { file.delete(); } } catch (Exception ex) {
	 * ex.printStackTrace(); } }
	 */

	public String getIsSale() {
		return isSale;
	}

	public void setIsSale(String isSale) {
		this.isSale = isSale;
	}

	public String getStartdate() {
		return startdate;
	}

	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}

	public String getEnddate() {
		return enddate;
	}

	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}

	public CardInfoQuery getCardInfoQuery() {
		return cardInfoQuery;
	}

	public void setCardInfoQuery(CardInfoQuery cardInfoQuery) {
		this.cardInfoQuery = cardInfoQuery;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getIstream() {
		return istream;
	}

	public void setIstream(InputStream istream) {
		this.istream = istream;
	}
}
